<template>
  <div class="container">
    <Save-Position ref="savePosition" @refresh="fetchData" />
    <a-card class="general-card" title="数据字典">
      <a-row class="grid-demo" :gutter="24">
        <a-col :span="8">
          <a-table
            :data="treeData"
            :show-header="false"
            :pagination="false"
            @row-click="getDict"
          >
            <template #columns>
              <a-table-column title="名称" data-index="name" />
            </template>
          </a-table>
        </a-col>
        <a-col :span="16">
          <a-table
            row-key="id"
            :loading="loading"
            :data="renderData"
            :pagination="false"
            column-resizable
            :hide-expand-button-on-empty="true"
            :bordered="false"
          >
            <template #columns>
              <a-table-column title="编码" data-index="dictValue" />
              <a-table-column title="名称" data-index="dictName" />
              <a-table-column title="备注" data-index="dictRemark" />
            </template>
          </a-table>
        </a-col>
      </a-row>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import { Message } from '@arco-design/web-vue';
  import { OrgVo } from '@/api/organization';
  import {
    PositionVo,
    selectPositionByOrgId,
    delPosition,
  } from '@/api/position';
  import {
    tDictPage,
    tDictTreePage,
    TDictTreeVO,
    TDictVO,
  } from '@/api/subsidy';

  const positionFormModel = () => {
    return {
      keywords: '',
      orgId: '',
    };
  };
  const { loading, setLoading } = useLoading();
  const formModel = ref(positionFormModel());
  const savePosition = ref();
  const renderData = ref<TDictVO[]>([]);
  const treeData = ref();

  const fetchData = async (parentId: string) => {
    setLoading(true);
    try {
      const { data } = await tDictPage({ page: 1, parentId, rows: 1000 });
      renderData.value = data.rows;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };
  const getDict = async (row: TDictTreeVO) => {
    fetchData(row.id);
  };
  const getTreeData = async () => {
    setLoading(true);
    try {
      const { data } = await tDictTreePage({ page: 1, rows: 1000 });
      treeData.value = data.rows;
    } catch (e) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const handleClickToSave = () => {
    savePosition.value.handleChangeVisible();
  };
  const handleClickToEdit = (obj: PositionVo) => {
    savePosition.value.handleChangeVisible(obj, 'edit');
  };
  getTreeData();
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
